package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.BlackList;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.mapper.BlackListMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class BlackListApiImpl implements BlackListApi {

    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 通过用户id分页查询黑名单列表
     *
     * @param loginUserId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPageByUserId(Long loginUserId, Long page, Long pageSize) {
        //1. 设置分页参数
        Page<BlackList> pageSetting = new Page<>(page, pageSize);
        //2. 构建查询条件
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUserId);
        // 按创建的时间降序，更好的用户体验
        queryWrapper.orderByDesc("created");
        //3. 分页查询
        IPage<BlackList> blackListPage = blackListMapper.selectPage(pageSetting, queryWrapper);
        //4. 构建pageResult再返回
        PageResult<BlackList> pageResult = new PageResult<>();
        // 总记录数
        pageResult.setCounts(blackListPage.getTotal());
        // 总页数
        pageResult.setPages(blackListPage.getPages());
        // 当前页码
        pageResult.setPage(page);
        // 每页大小
        pageResult.setPagesize(pageSize);
        // 分页结果集
        pageResult.setItems(blackListPage.getRecords());
        return pageResult;
    }

    /**
     * 移除黑名单
     *
     * @param loginUserId
     * @param blackUserId
     */
    @Override
    public void remove(Long loginUserId, Long blackUserId) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUserId)
                .eq("black_user_id",blackUserId);
        blackListMapper.delete(queryWrapper);
    }
}
